package org.teamh.gui.ingame;

import static org.lwjgl.opengl.GL11.*;

import org.teamh.gui.engine.model.Model;
import org.teamh.gui.engine.model.ModelLoader;
import org.teamh.gui.ingame.effects.Effect;

public class LetterAnimation extends Effect{

	private Model modelW;
	private Model modelI;
	private Model modelN;
	private Model modelE;
	private Model modelR;
	private Model modelL;
	private Model modelO;
	private Model modelS;
	private float tilePosX;
	private float tilePosZ;
	private String animationLetters;
	private float[] animationTime;
	private float start;
	private float end;
	private boolean stop;
	private float letterDistance;
	private float letterSize;
	private boolean afterStop;
	private float afterstopRotation;
	
	public LetterAnimation(float tilePosX, float tilePosZ, String animationLetters) {
		this.tilePosX = tilePosX;
		this.tilePosZ = tilePosZ;
		this.animationLetters = animationLetters;
		init();
	}

	private void init() {
		modelW = ModelLoader.getModel("res/model/letter/W.obj");
		modelI = ModelLoader.getModel("res/model/letter/I.obj");
		modelN = ModelLoader.getModel("res/model/letter/N.obj");
		modelE = ModelLoader.getModel("res/model/letter/E.obj");
		modelR = ModelLoader.getModel("res/model/letter/R.obj");
		modelL = ModelLoader.getModel("res/model/letter/L.obj");
		modelO = ModelLoader.getModel("res/model/letter/O.obj");
		modelS = ModelLoader.getModel("res/model/letter/S.obj");
		start = 4;
		end = 0.2f;
		animationTime = new float[animationLetters.length()];
		for(int i = 0; i < animationLetters.length(); i++) {
			animationTime[i] = start;
		}
		letterDistance = 0.5f;
		letterSize = 0.08f;
	}

	@Override
	public void render(float timeElapsed) {
		super.render(timeElapsed);
		
		glEnable(GL_LIGHTING);
		glEnable(GL_TEXTURE_2D);
		glColor3f(1, 1, 1);

		for(int i = 0; i < animationLetters.length(); i++) {
			if(!stop) {
				if(i == 0 || animationTime[i - 1] == end) {
					animationTime[i] -= timeElapsed * 0.01f;
					if(animationTime[i] <= end) {
						animationTime[i] = end;
					}
				}
			}	
			if(afterStop) {
				afterstopRotation += 1.0f;
				if(afterstopRotation >= 360.0f) {
					afterstopRotation = 0;
				}
			}
			
			if(animationTime[0] == end && animationTime[animationLetters.length() - 1] == end) {
				stop = true;
				afterStop = true;
			}
			
			glPushMatrix();
			{
				glTranslatef((tilePosX + ((i + 1) * letterDistance / (float)animationLetters.length())) - letterSize * letterDistance * animationLetters.length(), animationTime[i], tilePosZ);
				glRotatef(animationTime[i] * 45f + 90, 0, 1, 0);
				glRotatef(270, 0, 0, 1);
				glRotatef(((float)Math.sin(Math.toRadians(afterstopRotation))) * 45, 0, 1, 0);
				glRotatef(((float)Math.cos(Math.toRadians(afterstopRotation))) * 45, 1, 0, 0);
				glScalef(1/16f,1/16f,1/16f);
				getEffectManager().getIngameFunctions().getMap().setColor("ffffff");
				getModel(animationLetters.charAt(i)).render();
			}
			glPopMatrix();
		}	
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_LIGHTING);
		
		if(animationTime[0] == start && animationTime[animationLetters.length() - 1] == start) {
			getEffectManager().removeEffect(this);
		}
	}
	
	private Model getModel(char charAt) {
		Model m = null;
		switch (charAt)
		{	
		case 'W':
			m = modelW;
			break;
		case 'i':
			m = modelI;
			break;
		case 'n':
			m = modelN;
			break;
		case 'e':
			m = modelE;
			break;
		case 'r':
			m = modelR;
			break;
		case 'L':
			m = modelL;
			break;
		case 'o':
			m = modelO;
			break;
		case 's':
			m = modelS;
			break;
		}
		return m;
	}
}
